//
//  CollectionViewController.swift
//  TTProduct-macOS
//
//  Created by Toj on 5/12/21.
//

import Cocoa

class CollectionViewController: TTViewController {
    
    private lazy var scrollView: NSScrollView = {
        let view = NSScrollView()
        view.scrollerStyle = .overlay
        view.scrollerKnobStyle = .dark
        view.verticalScrollElasticity = .automatic
        view.documentView = collectionView
        return view
    }()
    
    private var testIdentifier = NSUserInterfaceItemIdentifier(rawValue: TTCollectionItem.className())
    private lazy var collectionView: NSCollectionView = {
        let view = NSCollectionView(frame: .zero)
        view.dataSource = self
        view.delegate = self
        view.backgroundColors = [NSColor.clear]
        
        let flowLayout = NSCollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        view.collectionViewLayout = flowLayout
        
        view.register(TTCollectionItem.self, forItemWithIdentifier: testIdentifier)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}


// MARK: - NSCollectionViewDataSource
extension CollectionViewController: NSCollectionViewDataSource {
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let item = collectionView.makeItem(withIdentifier: testIdentifier, for: indexPath)
        return item
    }
}

// MARK: - NSCollectionViewDelegate
extension CollectionViewController: NSCollectionViewDelegate {
    
}

// MARK: - setup
extension CollectionViewController: NSCollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
        return CGSize(width: 240, height: 135)
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
        
        return NSEdgeInsetsZero
    }
}

extension CollectionViewController {
    
    private func setupMakeAddSubviews() {
        view.addSubview(scrollView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        scrollView.snp.makeConstraints { (make) in
            make.top.bottom.left.equalTo(view)
            make.width.equalTo(240)
        }
    }
}
